//
//  NetworkRequest.m
//  NetworkRequest
//
//  Created by mac on 16/7/15.
//  Copyright © 2016年 www. All rights reserved.
//

#import "NetworkRequest.h"


@implementation NetworkRequest

+ (void)requestWithURL:(NSString *)url httpMethod:(NSString *)method params:(NSDictionary *)params fileData:(NSDictionary *)fileDic success:(SuccessBlock)sBlock failure:(FailureBlock )fBlock {

    //1.> 转化 url 接口
    NSURL *urlPath = [NSURL URLWithString:url];
    
    //2.> 构造 NSURLRequest 请求对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urlPath];
    
    //4.> 构造 NSURLSession 会话对象
    NSURLSession *session = [NSURLSession sharedSession];
    
    //5.> 构造 NSURLSessionDataTask 网络任务
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (error == nil) {
            
            id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            
            sBlock(result);
        } else {
            
            fBlock(error);
        }
        
    }];
    
    //6.> 发送网络对象 resume
    [dataTask resume];
}

@end
